<?php

/**
* This snippet loads a custom page-login.tpl.php layout file when
* users click through to the login, request password or register pages
*/
function _phptemplate_variables($hook, $variables = array()) {
  switch ($hook) {
    case 'page':
      global $user;
      if (arg(0) == 'user') {
        if (!$user->uid) { //check to see if the user is logged in. If not display the special login page layout
          $variables['template_file'] = 'page-login';
        }
        elseif (arg(1) == 'login' || arg(1) == 'register' || arg(1) == 'password' ) {
          $variables['template_file'] = 'page-login';
        }
      }
      break;
  }

  return $variables;
}

/**
 * Implementation of theme_menu_item() hook.
 */
function phptemplate_preprocess_page(&$vars) {
  // Disabling system css.
  if (!preg_match('/\bedit\b/i', $_GET['q']) && !preg_match('/\badmin\/build\/block\b/i', $_GET['q'])) {
    unset($vars['css']['all']['module']['modules/node/node.css']);
    unset($vars['css']['all']['module']['modules/system/defaults.css']);
    unset($vars['css']['all']['module']['modules/system/system.css']);
    unset($vars['css']['all']['module']['modules/system/system-menus.css']);
    unset($vars['css']['all']['module']['modules/user/user.css']);
    unset($vars['css']['all']['module']['sites/all/modules/fckeditor/fckeditor.css']);
    unset($vars['css']['all']['module']['sites/all/modules/cck/theme/content-module.css']);
    $vars['styles'] = drupal_get_css($vars['css']);  
  }
  
  // Add a section class in the body
  if (user_access('administer blocks')) {
    $body_classes[] = 'administrator';
  }
  if (!$vars['is_front']) {
    // Add unique classes for each page and website section
    $path = drupal_get_path_alias($_GET['q']);
    list($section, ) = explode('/', $path, 2);
    $body_classes[] = phptemplate_id_safe('page-'. $path);
    $body_classes[] = phptemplate_id_safe('section-'. $section);
  
    if (arg(0) == 'node') {
      if (arg(1) == 'add') {
        if ($section == 'node') {
          array_pop($body_classes); // Remove 'section-node'
        }
        $body_classes[] = 'section-node-add'; // Add 'section-node-add'
      }
      elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
        if ($section == 'node') {
          array_pop($body_classes); // Remove 'section-node'
        }
        $body_classes[] = 'section-node-'. arg(2); // Add 'section-node-edit' or 'section-node-delete'
      }
    }
  }
  $vars['body_classes'] = implode(' ', $body_classes); // Concatenate with spaces
  //IE8 Compatibility tag
  $vars['head'] = "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=7\" />\n". $vars['head'];
}

/**
 * Implementation of theme_menu_item() hook.
 */
function phptemplate_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
  // Set the menu class stack.
  global $_menu_stack;
  if (!isset($_menu_stack)) {
    $_menu_stack = array();
  }
  // Fix menu when has only one child item.
  if (substr_count($menu, '</li>') == 1) {
    $old_pos = (int) ereg_replace("(.*)(class=\".*lk)([[:digit:]+])(.*)", '\\3', $menu);
    $menu = ereg_replace("(.*)(class=\".*lk)([[:digit:]+])(.*)", '\\1\\21\\4', $menu);
    array_push($_menu_stack, (int) $old_pos - 1);
  }
  // Begin menu: insert value of the first class into the stack.
  if ($extra_class == 'first') {
    array_push($_menu_stack, (int) 0);
  }
  // Get the next menu class from stack.
  $pos = (int) array_pop($_menu_stack) + 1;

  // Save the menu class back into the stack.
  array_push($_menu_stack, $pos);
  // End menu: remove the class value from stack.
  if ($extra_class == 'last') {
    array_pop($_menu_stack);
  }
  // From theme_menu_item().
  $class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));
  // Insert span tag in menu item and mark as last when aplicable.
  if ($class != 'expanded' && $extra_class == 'last') {
    $link = ereg_replace("^(<a.*>)(.*)(</a>)$", '\\1\\2\\3', $link);
  }
  else {
    $link = ereg_replace("^(<a.*>)(.*)(</a>)$", '\\1\\2\\3', $link);
  }
  // Remove last item mark of child menu whose parent is not a last item.
  if ($class == 'expanded' && $extra_class != 'last') {
    $menu = ereg_replace("(<a.*>)()(.*</a>)", '\\1\\3', $menu);
  }
  // From theme_menu_item().
  if (!empty($extra_class)) {
    $class .= ' '. $extra_class;
  }
  if ($in_active_trail) {
    $class .= ' active-trail';
  }
  // Include menu class in li.
  return '<li class="lk'. $pos .' '. $class .'">'. $link . $menu ."</li>\n";
}

//
//  Converts a string to a suitable html ID attribute.
//
//   http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 specifies what makes a
//   valid ID attribute in HTML. This function:
//
//  - Ensure an ID starts with an alpha character by optionally adding an 'n'.
//  - Replaces any character except A-Z, numbers, and underscores with dashes.
//  - Converts entire string to lowercase.
//
//  @param $string
//    The string
//  @return
//    The converted string
//
function phptemplate_id_safe($string) {
  // Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
  $string = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
  // If the first character is not a-z, add 'n' in front.
  if ($string{0} == '-') {
    $string = 'id'. $string;
  }
  return $string;
}

//  from ZEN // Override or insert PHPTemplate variables into the node templates.
//
//   This function creates the NODES classes, like 'node-unpublished' for nodes
//   that are not published, or 'node-mine' for node posted by the connected user...
//
//  @param $vars
//    A sequential array of variables to pass to the theme template.
//  @param $hook
//    The name of the theme function being called ("node" in this case.)
function phptemplate_preprocess_node(&$vars, $hook) {
  global $user;
 
  // Special classes for nodes
  $node_classes = array();
  if ($vars['sticky']) {
    $node_classes[] = 'sticky';
  }
  if (!$vars['node']->status) {
    $node_classes[] = 'node-unpublished';
    $vars['unpublished'] = TRUE;
  }
  else {
    $vars['unpublished'] = FALSE;
  }
  if ($vars['node']->uid && $vars['node']->uid == $user->uid) {
  // Node is authored by current user
    $node_classes[] = 'node-mine';
  }
  if ($vars['teaser']) {
  // Node is displayed as teaser
    $node_classes[] = 'node-teaser';
  }
  // Class for node type: "node-type-page", "node-type-story", "node-type-my-custom-type", etc.
  $node_classes[] = 'node-type-'. $vars['node']->type;
  $vars['node_classes'] = implode(' ', $node_classes); // Concatenate with spaces
}